﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace DispatchAssist.AppHost
{
    public partial class frmChooseSkin : Form
    {
        Sunisoft.IrisSkin.SkinEngine skinEngine;

        public frmChooseSkin()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            SkinConfig.Instance.Enabled = this.ckEnableTheme.Checked;
            SkinConfig.Instance.SkinFile = this.cbSkins.Text;

            SkinConfig.Instance.UpdateSkin(this, skinEngine);

            SaveConfig("EnableSkin", SkinConfig.Instance.Enabled ? "1" : "0");
            SaveConfig("Skin", SkinConfig.Instance.SkinFile.Replace("Skins/", string.Empty));

            this.Close();
        }

        private void frmChooseSkin_Load(object sender, EventArgs e)
        {
            this.skinEngine = SkinConfig.Instance.EnsureSkin(this);
            this.ckEnableTheme.Checked = SkinConfig.Instance.Enabled;

            DirectoryInfo folder = new DirectoryInfo(System.Environment.CurrentDirectory + "\\Skins");
            foreach (FileInfo file in folder.GetFiles("*.ssk"))
            {
                this.cbSkins.Items.Add(file.Name);
                if (file.Name==SkinConfig.Instance.SkinFile.Replace("Skins/", string.Empty))
                {
                    this.cbSkins.SelectedIndex = this.cbSkins.Items.Count - 1;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SaveConfig(string key, string value)
        {
            XmlDocument doc = new XmlDocument();

            string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "Configs\\appSettings.config";
            doc.Load(strFileName);

            XmlNodeList nodes = doc.GetElementsByTagName("add");
            for (int i = 0; i < nodes.Count; i++)
            {
                XmlAttribute att = nodes[i].Attributes["key"];

                if (att.Value == key)
                {
                    att = nodes[i].Attributes["value"];
                    att.Value = value;
                    break;
                }
            }

            doc.Save(strFileName);
        }
    }
}
